#include <bits/stdc++.h>
#define sc(x) scanf("%lld", &(x))
#define pr(x) printf("%lld\n", (x))
#define rep(i, l, r) for (int i = l; i <= r; ++i)
using namespace std;
typedef long long ll;
const int N = 17;
const int mod = 1e9 + 7;
int n;
vector<int> G[N];

bitset<N> vis;
void dfs(int x) {
    vis[x] = 1;
    for (int son : G[x])
        if (son >= 0 && son <= 9) dfs(son);
}
bool chk() {
    for (int i = 0; i < n; ++i)
        if (!vis[i]) return 0;
    return 1;
}
int findRoot() {
    for (int i = 0; i < n; ++i) {
        vis.reset();
        dfs(i);
        if (chk()) return i;
    }
    return -1;
}
vector<int> res1, res2;
void bfs(int rt) {
    queue<int> q;
    q.push(rt);
    while (!q.empty()) {
        int x = q.front();
        if (x >= 0 && x <= 9) {
            res1.push_back(x);
            q.pop();
            for (int i : G[x])
                if (i >= 0 && i <= 9) q.push(i);
        }
    }
}
void inRoot(int x) {
    if (x >= 0 && x <= 9) {
        int sz = G[x].size();
        if (sz) inRoot(G[x][0]);
        res2.emplace_back(x);
        if (sz == 2) inRoot(G[x][1]);
    }
}
#define opt(a) \
    for (int i = 0; i < n; ++i) printf("%d%c", a[i], i == n - 1 ? '\n' : ' ');
int main() {
    cin >> n;
    cin.ignore();
    // build
    for (int i = 0; i < n; ++i) {
        string s;
        getline(cin, s);
        // reverse
        G[i].push_back(s.back() - '0');
        G[i].push_back(s.front() - '0');
    }

    int rt = findRoot();
    
    bfs(rt);
    inRoot(rt);
    opt(res1) opt(res2) return 0;
}